import Vue from 'vue'
import VueRouter from 'vue-router'
import dicts from './modules/dicts'
import news from './modules/news'
import publics from './modules/publics'
import imgs from './modules/imgs'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/welcome'
  },
  {
    path: '/main',
    component: () => import('@/views/Main/index.vue'),
    children: [
      {
        path: '/main',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: () => import('@/views/Welcome')
      },
      {
        path: '/hero',
        component: () => import('@/views/hero/index')
      },
      ...dicts,
      ...news,
      ...publics,
      ...imgs
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '/register',
    component: () => import('@/views/Register')
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!localStorage.token && to.path !== '/login' && to.path !== '/register') {
    next('/login')
  } else {
    next()
  }
})

export default router
